package com.paic.mhis.hcpms.agreement.healthcareprovider.util;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.paic.pafa.app.lwc.core.context.support.PafaCoreContexton;
import com.paic.pafa.appclient.ServicesProxyException;
import com.paic.pafa.logging2.Level;

/**
 * @Description 这是一个用来处理http相关行为的工具类：如获取request内容。
 * @author wh
 * @date 2014/5/5
 */

public class HttpProcessUtil {

	/**
	 * 获取内容
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	public static final String readRequestBody(HttpServletRequest request)
			throws UnsupportedEncodingException, IOException {
		String charset = request.getCharacterEncoding();
		if (charset == null) {
			charset = "utf-8";
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(
				request.getInputStream(), charset));
		CharArrayWriter data = new CharArrayWriter();
		char[] buf = new char[8192];
		int ret;
		while ((ret = in.read(buf, 0, 8192)) != -1) {
			data.write(buf, 0, ret);
		}
		return data.toString();
	}

	public static final MultipartFile getFile(String fileName,
			HttpServletRequest request) {
		// 得到multiRequest中的文件
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multiRequest.getFile(fileName);
		return file;
	}

	/**
	 * controller异常处理
	 */

	public static Map<String, Object> processException(Exception e) {
		PafaCoreContexton
				.getInstance()
				.getErrorLogger()
				.log("",
						Level.SEVERE,
						HttpProcessUtil.class.getName(),
						"ProcessException方法",
						e.getMessage() + ": "
								+ Arrays.toString(e.getCause().getStackTrace()),
						e.getCause(), null);
		Map<String, Object> result = new HashMap<String, Object>();

		if (e instanceof BusinessServiceException) {
			processBusinessServiceException(e, result);
		} else if (e instanceof ServicesProxyException) {
			processBusinessServiceException(e.getCause().getCause(), result);
		} else {
			processWebServiceException(result);
		}

		return result;
	}

	private static void processBusinessServiceException(Throwable e,
			Map<String, Object> result) {
		generateExceptionResult(result, Constants.STATE_KEY,
				Constants.FAILED_STATE_VALUE, Constants.MSG_KEY,
				Constants.MSG_DATABASE_EXCEPTION);
	}

	private static void processWebServiceException(Map<String, Object> result) {
		generateExceptionResult(result, Constants.STATE_KEY,
				Constants.REQUEST_PARAMETER_ERROR_STATE_VALUE,
				Constants.MSG_KEY, Constants.MSG_REQUEST_PARAMETER_ERROR);
	}

	public static void generateExceptionResult(Map<String, Object> result,
			String stateKey, String stateValue, String msgKey, Object msgValue) {
		result.put(stateKey, stateValue);
		result.put(msgKey, msgValue);
	}
}
